{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "84c72e00-479d-4e7f-bd68-2a320a28db58",
   "metadata": {},
   "source": [
    "# Lesson 5: Event-driven generation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5956349b-44e0-4982-87b6-f082a1d85b1c",
   "metadata": {},
   "source": [
    "### Import all needed packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddf42ac3",
   "metadata": {
    "height": 183
   },
   "outputs": [],
   "source": [
    "import boto3, os\n",
    "\n",
    "from helpers.Lambda_Helper import Lambda_Helper\n",
    "from helpers.S3_Helper import S3_Helper\n",
    "\n",
    "lambda_helper = Lambda_Helper()\n",
    "s3_helper = S3_Helper()\n",
    "\n",
    "bucket_name_text = os.environ['LEARNERS3BUCKETNAMETEXT']\n",
    "bucket_name_audio = os.environ['LEARNERS3BUCKETNAMEAUDIO']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f952067-8d25-408a-8af2-5175f39081ef",
   "metadata": {},
   "source": [
    "### Deploy your lambda function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e3430e0",
   "metadata": {
    "height": 998
   },
   "outputs": [],
   "source": [
    "%%writefile lambda_function.py\n",
    "\n",
    "#############################################################\n",
    "#\n",
    "# This Lambda function is written to a file by the notebook \n",
    "# It does not run in the notebook!\n",
    "#\n",
    "#############################################################\n",
    "\n",
    "import json\n",
    "import boto3\n",
    "import uuid\n",
    "import os\n",
    "\n",
    "s3_client = boto3.client('s3')\n",
    "transcribe_client = boto3.client('transcribe', region_name='us-west-2')\n",
    "\n",
    "def lambda_handler(event, context):\n",
    "    # Extract the bucket name and key from the incoming event\n",
    "    bucket = event['Records'][0]['s3']['bucket']['name']\n",
    "    key = event['Records'][0]['s3']['object']['key']\n",
    "\n",
    "    # One of a few different checks to ensure we don't end up in a recursive loop.\n",
    "    if key != \"dialog.mp3\": \n",
    "        print(\"This demo only works with dialog.mp3.\")\n",
    "        return\n",
    "\n",
    "    try:\n",
    "        \n",
    "        job_name = 'transcription-job-' + str(uuid.uuid4()) # Needs to be a unique name\n",
    "\n",
    "        response = transcribe_client.start_transcription_job(\n",
    "            TranscriptionJobName=job_name,\n",
    "            Media={'MediaFileUri': f's3://{bucket}/{key}'},\n",
    "            MediaFormat='mp3',\n",
    "            LanguageCode='en-US',\n",
    "            OutputBucketName= os.environ['S3BUCKETNAMETEXT'],  # specify the output bucket\n",
    "            OutputKey=f'{job_name}-transcript.json',\n",
    "            Settings={\n",
    "                'ShowSpeakerLabels': True,\n",
    "                'MaxSpeakerLabels': 2\n",
    "            }\n",
    "        )\n",
    "        \n",
    "    except Exception as e:\n",
    "        print(f\"Error occurred: {e}\")\n",
    "        return {\n",
    "            'statusCode': 500,\n",
    "            'body': json.dumps(f\"Error occurred: {e}\")\n",
    "        }\n",
    "\n",
    "    return {\n",
    "        'statusCode': 200,\n",
    "        'body': json.dumps(f\"Submitted transcription job for {key} from bucket {bucket}.\")\n",
    "    }\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7074c329",
   "metadata": {
    "height": 63
   },
   "outputs": [],
   "source": [
    "lambda_helper.lambda_environ_variables = {'S3BUCKETNAMETEXT' : bucket_name_text}\n",
    "lambda_helper.deploy_function([\"lambda_function.py\"], function_name=\"LambdaFunctionTranscribe\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9fbf4bd6",
   "metadata": {
    "height": 63
   },
   "outputs": [],
   "source": [
    "lambda_helper.filter_rules_suffix = \"mp3\"\n",
    "lambda_helper.add_lambda_trigger(bucket_name_audio, function_name=\"LambdaFunctionTranscribe\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3112f9d",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "s3_helper.upload_file(bucket_name_audio, 'dialog.mp3')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29a05dfa",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "s3_helper.list_objects(bucket_name_audio)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "586fea28-9219-43cb-b3cf-21e1ce295c06",
   "metadata": {},
   "source": [
    "#### Restart kernel if needed.\n",
    "- If you run the code fairly quickly from start to finish, it's possible that the following code cell `s3_helper.list_objects(bucket_name_text)` will give a \"Not Found\" error.  \n",
    "- If waiting a few seconds (10 seconds) and re-running this cell does not resolve the error, then you can restart the kernel of the jupyter notebook.\n",
    "- Go to menu->Kernel->Restart Kernel.\n",
    "- Then run the code cells from the start of the notebook, waiting 2 seconds or so for each code cell to finish executing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ba29ec0",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "s3_helper.list_objects(bucket_name_text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bde5d97f-f4a0-4bd2-85ed-923cd86017a1",
   "metadata": {},
   "source": [
    "#### Re-run \"download\" code cell as needed\n",
    "- It may take a few seconds for the results to be generated.\n",
    "- If you see a `Not Found` error, please wait a few seconds and then try running the `s3_helper.download_object` again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52f1ec85",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "s3_helper.download_object(bucket_name_text, 'results.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b43a9020",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "from helpers.Display_Helper import Display_Helper\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f32e2a02",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "display_helper = Display_Helper()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5fbe5612",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "display_helper.text_file('results.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79300f62",
   "metadata": {},
   "source": [
    "Extra resources:\n",
    "\n",
    "* [Generative AI code](https://community.aws/code/generative-ai)\n",
    "\n",
    "* [Generative AI](https://community.aws/generative-ai)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
